package jeecg.business.login;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jeecg.business.entity.site.SiteEntity;

import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.NameValuePair;
import org.jeecgframework.core.util.clean.SinaWeiboHtmlCleanUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.code.CodeUtil;
import org.jeecgframework.core.util.http.CommonFunc;
import org.jeecgframework.core.util.http.HttpClientUtil;

public class SinaWeiboLogin {
	
	
	
	public static void main(String[] args) {
		String username = "lin2528403@tom.com";
		String pwd = "123456a";
		DefaultHttpClient client = new DefaultHttpClient();
		
		HttpPost post = new HttpPost("https://passport.weibo.cn/sso/login");
		try{
			post.addHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E)");
			post.addHeader("Accept", "*/*");
			post.addHeader("Connection", "Keep-Alive");
			post.addHeader("Accept-Language", "zh-cn");
			post.addHeader("Accept-Encoding", "gzip, deflate");
			post.addHeader("Host", "passport.weibo.cn");
			post.addHeader("Accept-Language", "zh-cn");
			post.addHeader("Origin", "https://passport.weibo.cn");
			post.addHeader("Referer", "https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2Fhome%2Fsetting");
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("username", username));
			nvps.add(new BasicNameValuePair("password", pwd));
			nvps.add(new BasicNameValuePair("savestate", "1"));
			nvps.add(new BasicNameValuePair("entry", "mweibo"));
			nvps.add(new BasicNameValuePair("pagerefer", "https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2Fhome%2Fsetting&"));
			post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
			HttpResponse resp5 = client.execute(post);
			System.out.println("**** getTencentWeiboInfoByAccount() ---- resp5 responseCode="+resp5.getStatusLine().getStatusCode());
			InputStream in = resp5.getEntity().getContent();
			String searchResultHtml = CommonFunc.uncompress(in, "utf-8"); // 查询后显示的结果也
			System.out.println(searchResultHtml);
			String cookies = HttpClientUtil.getCookies(resp5);
			System.out.println("cookies:"+cookies);
			
			String url = "http://m.weibo.cn/page/pageJson?containerid=&containerid=100103type%3D1%26q%3D%E6%89%A7%E6%B3%95%E5%B1%80&type=all&page=1";
			String mystr = HttpClientUtil.getSourceFromUrl(url, "utf-8", cookies);
			mystr = mystr.replaceAll("\\%", "bai-fen-hao");		
			
			mystr = CodeUtil.unescape(mystr.replaceAll("\\\\u", "\\%u"));
			
			mystr = mystr.replaceAll("bai-fen-hao","\\%");
			System.out.println(mystr);
			
			
//			/**
//			 * 查询第二页
//			 */
//			String nextpage = "http://s.weibo.com/weibo/%25E6%2589%25A7%25E6%25B3%2595%25E5%25B1%2580&page=2";
//			String str = HttpClientUtil.getSourceFromUrl(nextpage, "utf-8", cookies);
//			System.out.println("第二页："+str);
//			
//			if(str.contains("location.replace")){
//				int position = str.indexOf("replace(\"")+"replace(\"".length();
//				
//				String nexturl = str.substring(position , str.indexOf("\")",position));
//				System.out.println(nexturl);
//				str = HttpClientUtil.getSourceFromUrl(nexturl, "utf-8", cookies);
//				System.out.println("第二页："+str);
//			}
//			HtmlParseResult result = SinaWeiboHtmlCleanUtil.fetchSinaBlog4Html(str, "utf-8", site, cookies);
//			
//			for(HtmlParseResult r : result.getChildResultList())
//				System.out.println(r);
			
			
//			String furl = "http://s.weibo.com/weibo/%25E6%2589%25A7%25E6%25B3%2595%25E5%25B1%2580";
//			str = HttpClientUtil.getSourceFromUrl(furl, "utf-8", null);
//			System.out.println("第一页："+str);
			
			
			// 释放get5
			post.releaseConnection();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			
		}
	}
	static SiteEntity site;
	
	static{
		site = new SiteEntity();
		site.setWyzfj("utf-8");
	}
}
